<?php
/**
 * SemesterDuration Model
 *
 * Utilizes the Data Mapper pattern to persist data. Represents a single
 * semester_duration entry
 *
 * @version $Id: SemesterDuration.php 123 2010-05-30 11:41:51Z yingkuang.chen $
 * @license New BSD License
 * @uses Semester_Model_SemesterDurationMapper
 * @package semester
 * @subpackage Model
 */
class Semester_Model_SemesterDuration extends Semester_Model_Doctrine_BaseSemesterDuration
{

    private $_tr ;

    private function _setTranslate()
    {
        if ($this->_tr)
        return;
        $locale = Bootstrap::$config['resources']['locale']['default'];
        $path = dirname(__FILE__)."/../languages/$locale/duration.php";
        $this->_tr = Zend_Registry::get('Zend_Translate');
        $this->_tr->addTranslation($path);
    }

    public function getYearSemesterOptions($selected='',$arr=array())
    {
        // 設定翻譯檔
        $this->_setTranslate();

        $date = date("Y-m-d");
        if (isset($arr['date']))
        $date = $arr['date'];
        $order = 'year ,semester';
        if (isset($arr['order']))
        $order = $arr['order'];

        $q = $this->getTable()->createQuery('a')
        ->from('Semester_Model_SemesterDuration')
        ->where('started_date<=?',$date)
        ->orderBy($order);

        $res = $q->fetchArray();
        $str = '';
        foreach($res as $val) {
            $id = $val['year'].'-'.$val['semester'];
            if ($selected == $id )
            $str .= sprintf("<option value='%s' selected='selected'>".$this->_tr->translate("%d year %d semester")."</option>\n",$id,$val['year'],$val['semester']);
            else
            $str .= sprintf("<option value='%s'>".$this->_tr->translate("%d year %d semester")."</option>\n",$id,$val['year'],$val['semester']);

        }
        return $str;
    }

    /**
     *
     * 取得目前學年學期
     */
    function get_curr_year_seme(){
        // 設定翻譯檔
        $this->_setTranslate();
        //以開始日設定回應
        $q = $this->getTable()->createQuery('a')
        ->where("started_date<=now()")
        ->orderby('started_date DESC');
        $temp = $q->fetchOne();

        if (!$temp) {
            $res = array();
            $res['semester'] = (date("m")>7)?1:2;
            if ($res['semester']==1)
            $res['year'] = date("Y")-1911;
            else
            $res['year'] = date("Y")-1912;
        }
        else
        $res = $temp->toArray();
        $res ['year_seme'] = sprintf("%03d%d",$res['year'],$res['semester']);
        $res ['yearSeme'] =  $res['year'].'-'.$res['semester'];
        $res['yearSemeName']  =  sprintf($this->_tr->translate("%d year %d semester"),$res['year'],$res['semester']);
        return $res;
    }

    /**
     * 取所有學年資料
     * @return array
     */
    function getAllYear($sort='DESC')
    {
        $this->_setTranslate();
        $arr = array();
        $q = $this->getTable()->CreateQuery('a')
        ->select('year')
        ->groupBy('year')
        ->orderBy('year '.$sort);
        $res = $q->fetchArray();
        foreach ($res as $row) {
            $arr[$row['year']] = sprintf($this->_tr->translate("%d year"),$row['year']);
        }

        return $arr ;

    }
    /**
     * 取得所有學期陣列
     *
     * @return array
     */
    function getAllYearSemester()
    {
        $this->_setTranslate();
        $arr = array();
        $q = $this->getTable()->createQuery('a')
        ->orderBy('year DESC');
        $res = $q->fetchArray();

        foreach($res as $row){
            $year_semester = sprintf("%d-%d",$row['year'],$row['semester']);
            $arr[$year_semester] =  sprintf($this->_tr->translate("%d year %d semester"),$row['year'],$row['semester']);
        }
        return $arr;
    }

}
